package com.work.customer.controller;

import com.github.pagehelper.Page;
import com.work.customer.pojo.CustUser;
import com.work.customer.service.CustUserService;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/custuser")
public class CustUserController {

    @Autowired
    private CustUserService custUserService;

    //修改平台超级管理员信息
    @PostMapping("/update")
    public Result update(@RequestBody CustUser custUser){
        try {
            custUserService.update(custUser);
            return new Result(true, StatusCode.UPDATE_Ok);
        }catch (Exception e){
            return new Result(true, StatusCode.UPDATE_ERROR);
        }

    }

    //多条件查询+分页查询
    @GetMapping(value = "/find")
    public Result findPage(@RequestParam(required = false) Map searchMap,
                           @RequestParam(name = "page",defaultValue = "1",required = false) int page,
                           @RequestParam(name = "size",defaultValue = "5",required = false) int size
                           ){
        try {
            Page findPage = custUserService.findPage(searchMap, page, size);
            return new Result(true, StatusCode.FIND_Ok,"查询成功",findPage);
        }catch (Exception e){
            return new Result(true, StatusCode.FIND_ERROR,"查询失败");
        }

    }

    //根据id查询
    @GetMapping(value = "/findById")
    public Result findById(@RequestParam("id") Integer id){
        try {
            CustUser custUser = custUserService.findById(id);
            return new Result(true, StatusCode.FIND_Ok,"查询成功",custUser);
        }catch (Exception e){
            return new Result(true, StatusCode.FIND_ERROR,"查询失败");
        }

    }
}
